package com.luck.pf.security.dept.logic;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.luck.org.entity.SysDepartment;
import com.luck.org.entity.SysLog;
import com.luck.org.entity.SysUser;
import com.luck.pf.common.Constants;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.logic.BaseLogic;
import com.luck.pf.security.dept.qry.DeptQry;
import com.luck.pf.util.CriteriaUtil;
import com.luck.pf.util.Page;

/**
 * 说明:
 * Copyright: Copyright (c) 2010
 * @author zhangwr 2011-8-10 下午02:40:13
 * @version 1.0
 */
public class DeptLogic extends BaseLogic implements IDeptLogic {
	/**
	 * 权限过滤
	 * @param criteria 查询对象
	 * @param orgAlias 过滤机构别名
	 */
	protected void filterDeptData(Criteria criteria,UserBean userInfo){
		if(userInfo.findResource(Constants.DATA_PRIV_ALL)){
			//查看所有数据	,不做过滤
		}else if(userInfo.findResource(Constants.DATA_PRIV_SELF_AND_BLEW)){
			//本机构数据及本机构以下数据
			CriteriaUtil.addRightLike(criteria, "treeNo", userInfo.getDept().getTreeNo());
		}else if(userInfo.findResource(Constants.DATA_PRIV_SELF_ORG)){
			//只能看本机构数据
			CriteriaUtil.addEq(criteria, "id", userInfo.getDeptId());
		}else if(userInfo.findResource(Constants.DATA_PRIV_SELF_DEPT)){
			//只能看本部门数据
			CriteriaUtil.addEq(criteria, "id", userInfo.getDeptId());
		}else if(userInfo.findResource(Constants.DATA_PRIV_SELF)){
			//只查看本人数据
			CriteriaUtil.addEq(criteria, "createdUser", userInfo.getUserId());
		}else{
			//啥数据也不查，随便给安一个机构ID值
			CriteriaUtil.addEq(criteria, "id", Long.valueOf(-9674));
		}
	}
	public boolean checkDept(SysDepartment dept){
		Criteria criteria = baseDao.createCriteria(SysDepartment.class);
		CriteriaUtil.addEq(criteria, "deptCode", dept.getDeptCode());
		List<SysDepartment> ls = criteria.list();
		if(ls != null && !ls.isEmpty()){
			if(dept.getId()==null || !dept.getId().equals(ls.get(0).getId()))
				return false;
		}
		return true;
	}
	public Page findDeptPage(DeptQry qry) throws Exception{
		Criteria criteria = super.baseDao.createCriteria(SysDepartment.class);
		CriteriaUtil.addFullLike(criteria, "deptCode", qry.getDeptCode());
		CriteriaUtil.addFullLike(criteria, "deptName", qry.getDeptName());
		CriteriaUtil.addEq(criteria, "deptStatus", qry.getDeptStatus());
		CriteriaUtil.addEq(criteria, "deptLevel", qry.getDeptLevel());
		criteria.addOrder(Order.asc("id"));
		filterDeptData(criteria, qry.getUserInfo());
		return baseDao.pagedQuery(criteria, qry.getPageNo(),qry.getPageSize());
	}
	public List<SysDepartment> findDepts(DeptQry qry) throws Exception {
		Criteria criteria = super.baseDao.createCriteria(SysDepartment.class);
		filterDeptData(criteria, qry.getUserInfo());
		return criteria.list();
	}

	public SysDepartment saveDept(SysDepartment dept,UserBean userBean) throws Exception {
		if(Constants.DEPT_MANY.equals("0")){
			if(dept.getParentId()!=null){
				SysDepartment parent = super.baseDao.get(SysDepartment.class, dept.getParentId());
				if(parent.getParentId()!=null)
					throw new Exception("不能增加多层机构！");
			}
		}
		
		if(dept.getId() == null && dept.getParentId() != null){
			SysDepartment parent = super.baseDao.get(SysDepartment.class, dept.getParentId());
			dept.setTreeNo(generatorTreeNo(parent));
			if(new Long(dept.getDeptLevel())<=new Long(userBean.getDept().getDeptLevel()))
				throw new Exception("机构级别必须低于您所属于机构");
		}
		super.baseDao.save(dept);
		return dept;
	}

	private String generatorTreeNo(SysDepartment parent) {
		List list = baseDao.find(" SELECT max(treeNo) FROM SysDepartment WHERE parentId=?",parent.getId());
		if(list == null ||list.isEmpty()){
			return parent.getTreeNo()+"001";
		}else{
			String treeno = (String) list.get(0);
			if(StringUtils.isBlank(treeno))
				return parent.getTreeNo()+"001";
			else{
				Long no = new Long(treeno);
				return (no+1)+"";
			}
		}
	}
	public String deleteDept(SysDepartment dept,UserBean userBean, SysLog log) throws Exception {
		StringBuffer bf = new StringBuffer();
		if(!userBean.getUser().getUserLevel().equals("1"))
			bf.append("您不是管理员<br/>");
		if(dept.getParentId() == null)
			bf.append("此为根机构<br/>");
		int suns = super.baseDao.count(" select count(*) from SysDepartment WHERE parentId=?", dept.getId());
		if(suns>0)
			bf.append("此机构下有子机构<br/>");
		if(userBean.getDeptId().equals(dept.getId()))
			bf.append("此为您所属机构<br/>");
		if(new Long(userBean.getDept().getDeptLevel())>= new Long(dept.getDeptLevel()))
			bf.append("此机构级别必须低于您所属机构<br/>");
		if(!dept.getTreeNo().startsWith(userBean.getDept().getTreeNo()))
			bf.append("此机构不是您下属机构<br/>");
		int users = super.baseDao.count(" select count(*) from SysUser WHERE deptId=?", dept.getId());
		if(users>0)
			bf.append("此机构下有用户<br/>");
		if(StringUtils.isBlank(bf.toString()))
			super.delete(dept);
		else
			throw new Exception(bf.toString());
		return bf.toString();
	}

	public List<SysDepartment> findDepts(Long parentId){
		return super.baseDao.find(" FROM SysDepartment WHERE parentId=?",parentId);
	}

}
